package jgl.vajra.kingkong.part.login.activity;

import android.os.Handler;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.LoginInfoBean;
import jgl.vajra.kingkong.constant.Constant;
import jgl.vajra.kingkong.databinding.ActivityLoginBinding;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.login.contract.LoginContract;
import jgl.vajra.kingkong.part.login.viewModel.LoginViewModel;
import jgl.vajra.kingkong.utils.CountUtils;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.bean.BaseRequestData;
import jgl.vajra.kingkong.widget.manager.SPManager;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.encrypt.RSAEncryptManager;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.PhoneUtils;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(LoginViewModel.class)
public class LoginActivity extends BaseMVVMActivity<LoginViewModel, ActivityLoginBinding> implements LoginContract.View {

    private boolean isAgree = false;
    private Boolean isDecryptRandomNumber = false;
    private String byInvitation;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        Handler handle = new Handler();
        handle.postDelayed(new Runnable() {
            @Override
            public void run() {
                //此处可放 调用获取剪切板内容的代码
                //获取验证码前设置邀请码
                byInvitation = PhoneUtils.getClipboardContent(LoginActivity.this);
            }
        }, 1000);

        requestNetData();
    }

    @Override
    public void initData() {
        super.initData();
    }

    public void requestNetData() {
        //获取AES密钥
        mViewModel.getAESSecretKey();
    }

    //取消
    @Override
    public void onDismiss() {
        finish();
        IntentManager.toHomeActivity(LoginActivity.this);
    }

    //用户协议
    @Override
    public void onUserAgreement() {
    }

    //隐私政策
    @Override
    public void onPrivacyPolicy() {

    }

    //同意
    @Override
    public void onAgreeCheck() {
        isAgree = !isAgree;
        SPManager.AgreementSplashData.saveAgreementStatus(true);
        mBinding.ivAgreeStatus.setSelected(isAgree);
    }

    //获取验证码
    @Override
    public void onVerifyCode() {
        //mBinding.etInvitation.setText(byInvitation);

        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }
        mBinding.tvVerifyCode.setClickable(false);
        getVerifyCode();
    }

    public void getVerifyCode() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(mBinding.etPhone.getText().toString().trim() + "&&" + RandomUtils.getRandom()));
        map.put("type", "register");
        mViewModel.getVerifyCode(map);
    }

    @Override
    public void onLogin() {
        if (!isAgree) {
            ToastUtils.showShort("请阅读并勾选下方协议");
            return;
        }

        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }

        String verifyCode = mBinding.etVerifyCode.getText().toString().trim();
        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        String encryptPhone = AEACCoder.encrypt(phone);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", encryptPhone);
        map.put("verificationCode", verifyCode);
        map.put("userType", Constant.USER_TYPE);
        if (!TextUtils.isEmpty(byInvitation))
            map.put("byInvitation", byInvitation);
        map.put("udid", PhoneUtils.getUDID());
        mViewModel.onLogin(map);


    }

    @Override
    public void returnLoginPasswordSuccess(LoginInfoBean data) {

        SPManager.LoginData.setIsLogin(true);

        IntentManager.toHomeActivity(LoginActivity.this);
        AccountManager.getInstance().saveLoginData(UIUtils.getContext(), data);
        finish();
    }

    //AES秘钥
    @Override
    public void returnAESSecretKeySuccess(String secretKey) {
        //RAS公有密钥:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxxeLN99tL26yZ7NSmZ73mo76kHFi/OXQy4E94zzVnD8o30ROjl9yT1N4QHterUFnxpbB6PTZU0fA68F+lHASVzkuQEEE+BchyjOKcQaBKT/7X7TNWjpYBUYHZ15Q8Sn4kCAvcGqhLkA33IkFdWNEoGwFHuey3BY/P3vNx2PrVT0mjYHh8hMtZzXZgbPcOam1Np5DEwtQZMBoQP6Vr+o0RQVNPFBchvwLJHChHXjq3QY+LPp0iUyNtn/AsZPekI0vMQlOQv1vZCjxKxU2ylfLHB5V0Y0JT+hEEhZ4/a5KNo0K1Ujb4gz026+qfAjOVajqTULe9zoXbQdXd2Mvq1SSVwIDAQAB
        //RAS私有密钥
        String rASPrivateKey = RSAEncryptManager.decryptByPublicString(secretKey);
        SPManager.HomeData.saveRASPrivateKey(rASPrivateKey);

    }

    @Override
    public void returnVerifyCodeData(int code) {
        if (code == 200) {
            CountUtils.getCount(mBinding.tvVerifyCode);
        } else {
            mBinding.tvVerifyCode.setClickable(true);
        }
    }
}
